import allure
from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from page.base_page import BasePage


class MainPage(BasePage):
    __close_pop = By.XPATH, "//*[@class = 'el-icon el-dialog__close' ]"
    __order_management = By.XPATH, "//*[text()='订单管理']"
    __proxy_order = By.XPATH, "//*[text()='代理订单']"
    __operated_order = By.XPATH, "//*[text()='自营订单']"
    __financial_management = By.XPATH, "//*[text()='财务管理']"
    __disbursement_account = By.XPATH, "//*[text()='放款虚户']"
    __platform_account = By.XPATH, "//*[text()='平台虚户']"
    __platform_account_detail = By.XPATH, '//*[@href="/financial/account/account/index"]'
    __disbursement_account_detail = By.XPATH, '//*[@href="/financial/disbursementAccount/finAccount"]'
    __proxy_order_list = By.XPATH, "//*[@href = '/order/proxyOrder/proxyOrderApply']"
    __self_order_list = By.XPATH, "//*[@href='/order/selfOrder/orderApply']"
    __approve_self_order = By.XPATH, "//*[@href='/order/selfOrder/ordercheck']"
    __history_order = By.XPATH, "//*[@href='/order/selfOrder/order']"
    __approve_proxy_order = By.XPATH, '//*[@href="/order/proxyOrder/proxyOrderCheck"]'
    __statistics_report = By.XPATH, "//*[text()='统计报表']"
    __proxy_report = By.XPATH, "//*[text()='代理报表']"
    __loan_statistics = By.XPATH, '//*[@href="/statistics/proxyReport/disburseStatisticsProxy"]'
    __repayment_statistics = By.XPATH, '//*[@href="/statistics/proxyReport/repayStatisticsProxy"]'
    __in_stock_statistics = By.XPATH, '//*[@href="/statistics/proxyReport/outstandingStatisticsProxy"]'
    __compensation_detail = By.XPATH, '//*[@href="/statistics/proxyReport/compensatoryDetails"]'
    __overdue_statistics = By.XPATH, '//*[@href="/statistics/proxyReport/overdueStatisticsProxy"]'

    def close_pop(self):
        try:
            with allure.step("尝试查找点击还款提醒关闭按钮"):
                self.find_and_click(self.__close_pop)
            return MainPage(self.driver)
        except NoSuchElementException:
            with allure.step("没有找到还款提醒关闭按钮"):
                pass
            return MainPage(self.driver)

    def goto_proxy_order(self):
        from page.proxy_order_apply_page import ProxyOrderApply
        with allure.step('点击订单管理'):
            self.find_and_click(self.__order_management)
        with allure.step('点击代理订单'):
            self.find_and_click(self.__proxy_order)
        with allure.step('点击订单列表'):
            self.find_and_click(self.__proxy_order_list)
            self.wait(EC.visibility_of_element_located((By.XPATH, "//*[text()='借贷人姓名']")), time_out=10)
        return ProxyOrderApply(self.driver)

    def goto_approve_proxy_order(self):
        from page.approve_proxy_order_page import ApproveProxyOrderPage
        with allure.step('点击订单管理'):
            self.find_and_click(self.__order_management)
        with allure.step('点击代理订单'):
            self.find_and_click(self.__proxy_order)
        with allure.step('点击审批订单'):
            self.find_and_click(self.__approve_proxy_order)
            self.wait(EC.visibility_of_element_located((By.XPATH, "//*[text()='借贷人姓名']")), time_out=10)
        return ApproveProxyOrderPage(self.driver)

    def goto_self_order(self):
        from page.order_apply import OrderApply
        with allure.step('点击订单管理'):
            self.find_and_click(self.__order_management)
        with allure.step('点击自营订单'):
            self.find_and_click(self.__operated_order)
        with allure.step('点击订单列表'):
            self.find_and_click(self.__self_order_list)
            self.wait(EC.visibility_of_element_located((By.XPATH, "//*[text()='借贷人姓名']")), time_out=10)
        return OrderApply(self.driver)

    def goto_approve_self_order(self):
        from page.approve_self_orders_page import ApproveSelfOrder
        with allure.step('点击订单管理'):
            self.find_and_click(self.__order_management)
        with allure.step('点击自营订单'):
            self.find_and_click(self.__operated_order)
        with allure.step('点击审批订单'):
            self.find_and_click(self.__approve_self_order)
            self.wait(EC.visibility_of_element_located((By.XPATH, "//*[text()='借贷人姓名']")), time_out=10)
        return ApproveSelfOrder(self.driver)

    def goto_history_order(self):
        from page.history_order_page import HistoryOrder
        with allure.step('点击订单管理'):
            self.find_and_click(self.__order_management)
        with allure.step('点击自营订单'):
            self.find_and_click(self.__operated_order)
        with allure.step('点击历史订单'):
            self.find_and_click(self.__history_order)
            self.wait(EC.visibility_of_element_located((By.XPATH, "//*[text()='借贷人姓名']")), time_out=10)
        return HistoryOrder(self.driver)

    def goto_disbursement_account_detail(self):
        from page.disbursement_account_page import DisbursementAccount
        with allure.step('点击财务管理'):
            self.find_and_click(self.__financial_management)
        with allure.step('点击放款虚户'):
            self.find_and_click(self.__disbursement_account)
        with allure.step('点击账户详情'):
            self.find_and_click(self.__disbursement_account_detail)
            self.wait(EC.visibility_of_element_located((By.XPATH, "//*[text()='配资方案']")), time_out=10)
        return DisbursementAccount(self.driver)

    def goto_platform_account_detail(self):
        from page.platform_account_detail_page import PlatformAccountDetail
        with allure.step('点击财务管理'):
            self.find_and_click(self.__financial_management)
        with allure.step('点击平台虚户'):
            self.find_and_click(self.__platform_account)
        with allure.step('点击账户详情'):
            self.find_and_click(self.__platform_account_detail)
        return PlatformAccountDetail(self.driver)

    def goto_loan_statistics(self):
        from page.loan_statistics_page import LoanStatistics
        with allure.step('点击统计报表'):
            self.find_and_click(self.__statistics_report)
        with allure.step('点击代理报表'):
            self.find_and_click(self.__proxy_report)
        with allure.step('点击放款统计'):
            self.find_and_click(self.__loan_statistics)
        return LoanStatistics(self.driver)

    def goto_repayment_statistics(self):
        from page.repayment_statistics_page import RepaymentStatistics
        with allure.step('点击统计报表'):
            self.find_and_click(self.__statistics_report)
        with allure.step('点击代理报表'):
            self.find_and_click(self.__proxy_report)
        with allure.step('点击还款统计'):
            self.find_and_click(self.__repayment_statistics)
        return RepaymentStatistics(self.driver)

    def goto_in_stock_statistics(self):
        from page.in_stock_statistics_page import InStockStatistics
        with allure.step('点击统计报表'):
            self.find_and_click(self.__statistics_report)
        with allure.step('点击代理报表'):
            self.find_and_click(self.__proxy_report)
        with allure.step('点击在库统计'):
            self.find_and_click(self.__in_stock_statistics)
        return InStockStatistics(self.driver)

    def goto_compensation_detail(self):
        from page.compensation_detail_page import CompensationDetail
        with allure.step('点击统计报表'):
            self.find_and_click(self.__statistics_report)
        with allure.step('点击代理报表'):
            self.find_and_click(self.__proxy_report)
        with allure.step('点击代偿明细报表'):
            self.find_and_click(self.__compensation_detail)
        return CompensationDetail(self.driver)

    def goto_overdue_statistics(self):
        from page.overdue_statistics_page import OverdueStatistics
        with allure.step('点击统计报表'):
            self.find_and_click(self.__statistics_report)
        with allure.step('点击代理报表'):
            self.find_and_click(self.__proxy_report)
        with allure.step('点击逾期统计'):
            self.find_and_click(self.__overdue_statistics)
        return OverdueStatistics(self.driver)
